package jt.classic.app.gdzc;

import jt.classic.app.gdzc.impl.GdzcTemplateImpl;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.databook.DataIndexManager;
import jt.classic.system.databook.IDataIndex;
import jt.classic.system.user.IUser;
import jt.classic.system.user.UserManager;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.basework.Column;
import org.limp.basework.Table;
import org.limp.basework.analyzer.TableRegisterCenter;
import org.limp.basework.impl.CommonBean4HSQ;

import org.limp.mine.StringTool;
import org.limp.mine.annex.AnnexInfo;

import java.sql.Connection;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 固定资产模板管理
 *
 * @author mido
 *
 */
public class GdzcTemplateAction extends SimpleAction {
    /**
     * 模板设计
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward design(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map param = cbh.getResource();

        try {
            SystemDataSource ds = SystemDataSource.getInstance();

            conn = ds.getConnection();

            DataIndexManager dim = new DataIndexManager(conn);

            /*固定资产分类根节点*/
            IDataIndex dataIndex = dim.getDataIndexByKey("gdzc");

            request.setAttribute("dataIndex", dataIndex);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("design");
    }

    /**
     * 模板使用授权
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward purview(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map param = cbh.getResource();

        try {
            SystemDataSource ds = SystemDataSource.getInstance();

            conn = ds.getConnection();

            DataIndexManager dim = new DataIndexManager(conn);

            /*固定资产分类根节点*/
            IDataIndex dataIndex = dim.getDataIndexByKey("gdzc");

            request.setAttribute("dataIndex", dataIndex);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("purview");
    }

    /**
     * 模板维护管理
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward manage(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map param = cbh.getResource();

        try {
            SystemDataSource ds = SystemDataSource.getInstance();

            conn = ds.getConnection();

            DataIndexManager dim = new DataIndexManager(conn);

            /*固定资产分类根节点*/
            IDataIndex dataIndex = dim.getDataIndexByKey("gdzc");

            request.setAttribute("dataIndex", dataIndex);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("manage");
    }

    /**
     * 模板定义
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward neu(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map param = cbh.getResource();

        String id = (String) param.get("id");

        try {
            SystemDataSource ds = SystemDataSource.getInstance();

            conn = ds.getConnection();

            DataIndexManager dim = new DataIndexManager(conn);

            IDataIndex dataIndex = dim.getDataIndexById(id);

            request.setAttribute("dataIndex", dataIndex);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("neu");
    }

    /**
     * 模板编辑
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map param = cbh.getResource();

        String id = (String) param.get("id");

        try {
            SystemDataSource ds = SystemDataSource.getInstance();

            conn = ds.getConnection();

            GdzcTemplateManager gtmanager = new GdzcTemplateManager(conn);
            GdzcTemplate template = gtmanager.getTemplateById(id);

            request.setAttribute("template", template);

            DataIndexManager dim = new DataIndexManager(conn);

            IDataIndex dataIndex = dim.getDataIndexById(template.itypeid());

            request.setAttribute("dataIndex", dataIndex);
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("edit");
    }

    /**
     * 加载模块项
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward loaditem(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map param = cbh.getResource();

        List resultList = new ArrayList();

        String tableName = StringTool.checkString(param.get("tn"));

        TableRegisterCenter center = TableRegisterCenter.getInstance();

        if (center.containsTable(tableName)) {
            Table table = center.findTable(tableName);

            List columnList = table.columns();

            if ((columnList != null) && !columnList.isEmpty()) {
                for (int i = 0; i < columnList.size(); i++) {
                    Map temp = new HashMap();

                    Column column = (Column) columnList.get(i);

                    if (tableName.equals("APP_GDZC_OBJECT") ||
                            tableName.equals("APP_GDZC_WORDBOOK") ||
                            tableName.equals("APP_GDZC_DATAINDEX") ||
                            tableName.equals("APP_GDZC_TABLEQUERY")) {
                        if (!column.isPremaryKey() &&
                                (column.getName().indexOf("_ID") == -1) &&
                                (column.getName().indexOf("_TYPE") == -1)) {
                            temp.put("name", column.getName());
                            temp.put("memo", column.getDescription());
                            temp.put("tp", column.getType().toString());

                            resultList.add(temp);
                        }
                    } else {
                        if (!column.isPremaryKey()) {
                            temp.put("name", column.getName());
                            temp.put("memo", column.getDescription());
                            temp.put("tp", column.getType().toString());

                            resultList.add(temp);
                        }
                    }
                }
            }
        }

        JSONArray jsonArray = JSONArray.fromObject(resultList);

        String jsonString = "{\"totalCount\":\"" + resultList.size() +
            "\",\"columnList\":";
        jsonString += jsonArray.toString();
        jsonString += "}";

        request.setAttribute("result", jsonString);

        return mapping.findForward("jsonresult");
    }

    /**
     * 模板编辑
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward loadcomponents(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        if (ISystem.invalidSesstion(request)) {
            IUser user = UserManager.createGuestUser();

            ISystem.createSessionUser(request, user);
        }

        IUser user = ISystem.getSessionUser(request);

        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map param = cbh.getResource();

        param.put("sessionUserLogid", user.ilogid());
        param.put("sessionUserId", user.id());
        param.put("sessionUserName", user.iname());
        param.put("sessionGroupId", user.igroup().id());
        param.put("sessionGroupName", user.igroup().iname());
        param.put("sessionGroupNumber", user.igroup().inumber());

        String tempid = (String) param.get("tempid");

        try {
            SystemDataSource ds = SystemDataSource.getInstance();

            conn = ds.getConnection();

            GdzcTemplateManager gtmanager = new GdzcTemplateManager(conn);
            GdzcTemplate template = gtmanager.getTemplateById(tempid);

            String jsonString = GdzcTemplateManager.getTemplateJson(template,
                    param);

            request.setAttribute("result",
                "{success: true, data:" + jsonString + "}");
        } catch (Exception ex) {
            ISystem.catchException(request, this, ex);
            request.setAttribute("result", "{success: false}");
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("jsonresult");
    }
}
